package com.starblink.android.basic.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.starblink.basic.style.R
import com.starblink.android.basic.databinding.ViewMoreCircleBinding
import com.starblink.android.basic.ext.dp2px
import com.starblink.basic.ext.load
import com.starblink.basic.util.CommUtils

/**
 * 商品、店铺 logo 视图（兼容无url时展示名称首字母）
 */
class MoreCircleHeadView @JvmOverloads constructor(
    context: Context,
    attrSet: AttributeSet?,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrSet, defStyleAttr) {

    private val binding: ViewMoreCircleBinding

    init {
        binding = ViewMoreCircleBinding.inflate(LayoutInflater.from(context), this, true)
        val typedArray = context.obtainStyledAttributes(
            attrSet,
            R.styleable.CircleLogoView
        )
        typedArray.recycle()
    }


    override fun onFinishInflate() {
        super.onFinishInflate()
    }

    public fun load(
        logos: List<String>,
        size: Float = 20f,
        borderColor: Int = R.color.sk_gray0
    ) {
        binding.content.removeAllViews()
        for ((index, logo) in logos.withIndex()) {
            val imageView = CircleImageView(context)
            imageView.load(logo)
            imageView.borderColor = CommUtils.getColor(borderColor)
            imageView.borderWidth = 1
            binding.content.addView(imageView)

            var layoutParams: LayoutParams = imageView.layoutParams as LayoutParams
            layoutParams.width = dp2px(size)
            layoutParams.height = dp2px(size)

            layoutParams.setMargins(dp2px(index * (size / 2)), 0, 0, 0)
        }
    }

}